﻿using B.S.RbacData.Domain;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.RBAC;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.RBAC
{

    /// <summary>
    /// 用户逻辑删除命令处理
    /// </summary>
    public class IsDelUserCommandhandlter : IRequestHandler<IsDelUserCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<UserModel> userRep;

        public IsDelUserCommandhandlter(IBaseRepository<UserModel> userRep)
        {
            this.userRep = userRep;
        }

        public async Task<ApiResult<int>> Handle(IsDelUserCommand request, CancellationToken cancellationToken)
        {


            //查询用户信息
            var user = userRep.GetAll().AsNoTracking().FirstOrDefault(x => !x.IsDeleted && x.ID == request.UserId);
            user.IsDeleted = true;
            //逻辑删除用户信息
            await userRep.UpdateAsync(user);

            return new ApiResult<int>()
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "删除成功"


            };
        }
    }
}
